package com.tech.connect.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.ksy.common.activity.CommonBaseActivity;
import com.ksy.common.utils.AndroidUtil;
import com.tech.connect.R;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * Created by Ksy.
 */

public class BaseActivity extends CommonBaseActivity {
    protected BaseActivity activity;

    @Override
    protected void onDestroy() {
        dispose();
        super.onDestroy();
    }

    @Override
    protected void initImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .fitsSystemWindows(false)
                .keyboardEnable(true)
                .init();
    }

    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        if (getHeadBar() != null) {
            getHeadBar().setTv_titleColor(R.color.common_colorWhite);
            getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            getHeadBar().setBackgroundResource(R.drawable.icon_headbar);
            if (isFullStateBar()) {
                getHeadBar().setPadding(0, AndroidUtil.dip2px(25), 0, 0);
            }
        }
    }

    //将所有正在处理的Subscription都添加到CompositeSubscription中。统一退出的时候注销观察
    private CompositeDisposable mCompositeDisposable;

    public void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null || mCompositeDisposable.isDisposed()) { //csb 如果解绑了的话添加 sb 需要新的实例否则绑定时无效的
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    //在界面退出等需要解绑观察者的情况下调用此方法统一解绑，防止Rx造成的内存泄漏
    protected void dispose() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }

}
